<template>
  <view></view>
</template>
<script lang="ts" setup>
import { setSessionCode } from '@/storage/global'
import { INF_BindedHotels_res } from '@/api/group/user'
import { onLoad } from '@dcloudio/uni-app'
import toast from '@/utils/toast'
import { INF_jscodeLogin_res, jscodeLogin } from '@/api/group/login'
import { BindedHotels } from '@/api/group/user'
import route from '@/utils/route'
import { loginPage_hotelList, loginPage_register } from '@/route'

const handleLogin = function (jscode: string) {
  jscodeLogin({ jscode }).then((res) => {
    const SessionCode: string = res as INF_jscodeLogin_res
    setSessionCode(SessionCode)
    BindedHotels({ sessioncode: SessionCode }).then((list) => {
      const HotelInfos = list as INF_BindedHotels_res
      if (HotelInfos?.length > 0) {
        route.$route_redirect(loginPage_hotelList)
      } else {
        route.$route_redirect(loginPage_register)
      }
    })
  })
}

onLoad(() => {
  //   #ifdef MP-WEIXIN
  uni.login({
    provider: 'weixin',
    onlyAuthorize: true, // 微信登录仅请求授权认证
    success(event) {
      const { code } = event
      //客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
      handleLogin(code)
    },
    fail(err) {
      // 登录授权失败
      // err.code是错误码
      toast.$toast(err)
    }
  })
  // #endif

  // #ifdef H5
  handleLogin('123654')
  // #endif;
  uni.hideTabBar({
    fail: () => {
      // 捕获报错，防止没有tabbar页面调用后控制台报错
    }
  })
})
</script>
